<?php
/**
 * 百度音乐
 * @author Tongle Xu <xutongle@gmail.com> 2013-9-24
 * @copyright Copyright (c) 2003-2103 tintsoft.com
 * @license http://www.tintsoft.com
 * @version $Id$
 */
class Music{
	private $http;

	public function __construct(){
		$this->http = new HttpClient();
	}

	/**
	 * 搜索歌曲
	 * @param string $word 歌曲名称
	 */
	public function getInfo($word){
		$result = $this->http->get('http://mp3.baidu.com/dev/api/?tn=getinfo&ct=0&word='.urlencode($word).'&ie=utf-8&format=json');
		if($result->code() == 200){
			$res = $result->data();
			return json_decode($res,true);
		}
		return false;
	}

	/**
	 * 获取歌曲详细信息
	 * @param int $song_id
	 */
	public function getMusic($song_id){
		$result = $this->http->get('http://ting.baidu.com/data/music/links?songIds='.$song_id);
		if($result->code() == 200){
			$res = json_decode($result->data(),true);
			if($res['errorCode']==22000 && isset($res['data']['songList']['0'])){
				$return = $res['data']['songList']['0'];
				unset($return['artistId'],$return['albumId'],$return['albumName'],$return['version'],$return['linkCode'],$return['relateStatus'],$return['resourceType'],$return['copyType']);
				$return['lrcLink'] = 'http://ting.baidu.com'.$return['lrcLink'];
				return $return;
			}
		}
		return false;
	}
}